package com.guo.hui.eurekahystrix;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName HystrixController
 * @Description TODO
 * @Author zhengguohui
 * @Date 2018/7/18 14:12
 */

@RestController
public class HystrixController {

    @Autowired
    ConsumerService consumerService;
    @Autowired
    EurekaHystrixClient eurekaHystrixClient;

    @RequestMapping("/feign")
    public String feign(String name) {
        String result = eurekaHystrixClient.hello(name);
        return result;
    }


    @RequestMapping("/hystrix")
    public String hystrix(String name) {
        String ss = "hello" + name;
        return consumerService.consumer();
    }

    @Component
    class ConsumerService {

        @Autowired
        RestTemplate restTemplate;

        @HystrixCommand(defaultFallback = "fallback")
        public String consumer() {
            return restTemplate.getForObject("http://eureka-client/hello?name=world", String.class);
        }

        public String fallback() {
            return "fallback";
        }


    }


}
